package com.order.controller.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.order.config.Consts;
import com.order.interceptor.business.AccountInterceptor;
import com.order.model.Dish;

/** 
* @ClassName: CartController 
* @Description: 购物车控制器
* @author LinBo
* @date 2014年4月25日 下午6:01:39 
*  
*/ 

@Before(AccountInterceptor.class)
@ControllerBind(controllerKey = Consts.BUSINESS + "cart")
public class CartController extends Controller {

	/** 
	* @Title: toCart 
	* @Description:    定位到购物车
	*/ 
	public void toCart(){
		String cuisineDishId = getPara("cuisine_dish_id","");
		if(StringKit.notBlank(cuisineDishId)){
			//添加到购物车
			addToCart(cuisineDishId);
		}
		//查询购物车菜品
		System.out.println("=============遍历前:"+getSessionAttr(Consts.CART));
		Map<String, Integer> cart = getSessionAttr(Consts.CART);
		System.out.println("=============遍历前:"+cart);
		List<Record> list = getCartDish(cart);
		System.out.println("====购物车中的菜品："+list);
		setAttr("dishs", list);
		//计算总价格
		double total = Dish.dao.getSum(cart);
		setAttr("total", total);
		render(Consts.BUSINESS + "cart.htm");
	}
	
	/** 
	* @Title: addToCart 
	* @Description:    添加到购物车
	*/ 
	public void addToCart(){
		String msg = "true";
		String cuisineDishId = getPara("cuisine_dish_id");
		System.out.println("cuisineDishId:"+cuisineDishId);
		if(addToCart(cuisineDishId)){
			msg = "notOnSell";
		}
		setAttr("msg", msg);
		renderJson();
	}
	/** 
	* @Title: addToCart 
	* @描述: 添加到购物车动作
	* @param cuisineDishId
	* @return   
	*/ 
	private boolean addToCart(String cuisineDishId){
		try{
			if(StringKit.notBlank(cuisineDishId) && Dish.dao.isOnSell(cuisineDishId)){
				//从session中取出购物车对象(cuisine_dish_id,数量)
				Map<String, Integer> cart = getSessionAttr(Consts.CART);
				if(cart == null){
					cart = new HashMap<String, Integer>();
					cart.put(cuisineDishId, 0);
				}
				Integer num = cart.get(cuisineDishId);
				if(num == null){
					num = 0;
				}
				//增加购物车
				cart.put(cuisineDishId, num + 1);
				setSessionAttr(Consts.CART, cart);
				System.out.println("=============购物车:"+getSessionAttr(Consts.CART));
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/** 
	* @Title: getCartDish 
	* @描述: 获取购物车菜品
	* @param cart
	* @return   
	*/ 
	public List<Record> getCartDish(Map<String, Integer> cart) {
		List<Record> list = new ArrayList<Record>();
		if(cart != null && !cart.isEmpty()){
			System.out.println(cart);
			Iterator it = cart.keySet().iterator();
			while(it.hasNext()){
				Integer cuisineDishId = Integer.valueOf(it.next().toString());
				String sql = " select d.id dish_id, d.name,d.price, d.info, d.image, cd.id cuisine_dish_id"
						+ " from dish d, cuisine_dish cd "
						+ " where cd.dish_id = d.id "
						+ " and cd.id = ? ";
				Record record = Db.findFirst(sql, cuisineDishId);
				System.out.println("----------cuisineDishId:"+cuisineDishId);
				System.out.println("----cart----------"+cart);
				System.out.println("==============="+cart.get(String.valueOf(cuisineDishId)));
				record.set("quantity", cart.get(String.valueOf(cuisineDishId)));
				double sum = cart.get(String.valueOf(cuisineDishId)) * record.getNumber("price").doubleValue();
				record.set("sum", sum);
				list.add(record);
				System.out.println("--------------record:"+record);
			}
		}
		System.out.println("=========list:"+list);
		return list;
	}
}
